<?php

namespace App\Http\Controllers;

use GuzzleHttp\Client;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

class CommonController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    // 获取用户真是ip
    public function getip(Request $request)
    {
        $ip = $request->ip();
        // GET请求高德api
        $client = new Client();
        $response = $client->request('GET', 'http://restapi.amap.com/v3/ip?key=1f4c574041f30b37f07a0e819821420a&ip=' . $ip);
        $contents = (string) $response->getBody();
        $contents = json_decode($contents);
        return response()->json([
            'code' => 200,
            'msg' => 'ok',
            'ip' => $ip,
            'data' => $contents,

        ]);
    }

    // 获取唯一id
    public function getuniqid(Request $request)
    {
        $id = uniqid();
        return response()->json([
            'code' => 200,
            'msg' => 'ok',
            'data' => $id,
        ]);
    }
    // 获取验证码
    public function getyzm(Request $request)
    {
        return response()->json([
            'code' => 200,
            'data' => app('captcha')->create('default', true),
        ]);
    }
    // 校验验证码
    public function verifyzm(Request $request)
    {
        $v_code = $request->input('code'); //验证码
        $key = $request->input('key'); //key
        if (captcha_api_check($v_code, $key)) {

            return response()->json([
                'code' => 200,
                'msg' => 'ok',

            ]);
        } else {
            return response()->json([
                'code' => 201,
                'msg' => '验证失败',

            ]);
        }
    }
    //随机头像
    public function avatar(Request $request)
    {

        $sort = $request->input('sort');
        $client = new Client();
        $response = $client->request('GET', 'http://api.uomg.com/api/rand.avatar?format=json&sort=' . $sort);
        $contents = $response->getBody();
        return $contents;

    }

    //网易云音乐
    public function wyyneran(Request $request)
    {

        $client = new Client();
        $response = $client->request('GET', 'http://api.uomg.com/api/rand.music?sort=热歌榜&format=json');
        $contents = (string) $response->getBody();
        $contents = json_decode($contents);
        return response()->json([
            'code' => 200,
            'msg' => 'ok',
            'data' => $contents,

        ]);
    }
    //翻译
    public function trans(Request $request)
    {

        $content = $request->input('content');
        $client = new Client();
        $response = $client->request('GET', 'http://api.wpbom.com/api/trans.php?msg=' . $content);
        $contents = $response->getBody();
        return $contents;

    }

    //情话
    public function qinghua(Request $request)
    {

        $client = new Client();
        $response = $client->request('GET', 'http://api.uomg.com/api/rand.qinghua?format=json');
        $contents = $response->getBody();
        return $contents;

    }
    //美女
    public function nvpu(Request $request)
    {

        $sort = $request->input('fenlei');
        $client = new Client();
        $response = $client->request('GET', 'http://api.uomg.com/api/rand.img4?format=json&sort=' . $sort);
        $contents = $response->getBody();
        return $contents;

    }
    //二维码
    public function ewm(Request $request)
    {

        $url = $request->input('url');
        $client = new Client();
        $response = $client->request('GET', 'http://api.uomg.com/api/qrcode?url=' . $url);
        $contents = $response->getBody();
        return $contents;

    }
    //手机归属地
    public function getphone(Request $request)
    {

        $phone = $request->input('phone');
        $client = new Client();
        $response = $client->request('GET', 'http://api.vvhan.com/api/phone?tel' . $phone);
        $contents = $response->getBody();
        return $contents;

    }

}
